/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import java.sql.*;
import java.util.ArrayList;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import javax.faces.event.ActionEvent;

public class Rezervacija implements Comparable{

    private int rid;
    private String sala="";
    private String namena="";
    private String datum="";
    private String datum_rezervacije;
    private String pocev_od="";
    private String trajanje;
    private Time start;
    private Time end;
    private String starts,ends;
    private boolean free=false;
    private String korisnik;
    private Date dat;
    public Rezervacija(){}

    public Rezervacija(Time s,Time e,String k,String n,boolean f,Date d){//ZA SVAKI DAN
        start=s;
        end=e;
        starts=start.toString();
        ends=end.toString();
        korisnik=k;
        namena=n;
        free=f;
        dat=d;
        datum=d.toString();
    }

    public Rezervacija(int r,String n,String d,String dr,String po,String t){//ZA SVAKI DAN
        rid=r;
        namena=n;
        datum=d;
        datum_rezervacije=dr;
        pocev_od=po;
        trajanje=t;
    }

    public String toString(){
        String ret="Vreme rezervacije" + start.toString() + " "  + end.toString();
        if(korisnik!=null)ret+=" " + korisnik;
        if(namena!=null)ret+=" " + namena;
        if(getSala()!=null)ret+=" " + getSala();
        return ret;
    }

    public void set_datum(){
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);

        Datum date = (Datum)session.getAttribute("datum");
        date.setDan(""+getDat().getDate());
        date.setMesec(""+(getDat().getMonth()+1));
        date.setGodina(""+(getDat().getYear()+1900));
        date.setTod(""+start.getHours());
        date.setTdo(""+end.getHours());
    }

    public String kprez(){
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        Datum date = (Datum)session.getAttribute("datum");
        date.setDan(""+getDat().getDate());
        date.setMesec(""+(getDat().getMonth()+1));
        date.setGodina(""+(getDat().getYear()+1900));
        date.setTod(""+start.getHours());
        date.setTdo(""+end.getHours());
        User u=(User)session.getAttribute("user");
        return u.kprez();
    }

    public Time getStart() {
        return start;
    }

    /**
     * @param start the start to set
     */
    public void setStart(Time start) {
        this.start = start;
    }

    /**
     * @return the end
     */
    public Time getEnd() {
        return end;
    }

    /**
     * @param end the end to set
     */
    public void setEnd(Time end) {
        this.end = end;
    }

    public String getStarts() {
        return starts;
    }

    /**
     * @param starts the starts to set
     */
    public void setStarts(String starts) {
        this.starts = starts;
    }

    public boolean getFree() {
        return free;
    }

    /**
     * @param starts the starts to set
     */
    public void setFree(boolean free) {
        this.free = free;
    }

    /**
     * @return the ends
     */
    public String getEnds() {
        return ends;
    }

    /**
     * @param ends the ends to set
     */
    public void setEnds(String ends) {
        this.ends = ends;
    }

    public int getRid() {
        return rid;
    }

    public void setRid(int rid) {
        this.rid = rid;
    }

    public String getSala() {
        return sala;
    }

    public void setSala(String sala) {
        this.sala = sala;
    }

     public String getNamena() {
        return namena;
    }

    public void setNamena(String username) {
        this.namena = username;
    }
     public String getDatum() {
        return datum;
    }

    public void setDatum(String username) {
        this.datum = username;
    }
     public String getDatum_rezervacije() {
        return datum_rezervacije;
    }
    public Date getDat() {
        return dat;
    }

    public void setDat(Date dat) {
        this.dat = dat;
    }


    public void setDatum_rezervacije(String datum_rezervacije) {
        this.datum_rezervacije = datum_rezervacije;
    }
     public String getPocev_od() {
        return pocev_od;
    }

    public void setPocev_od(String username) {
        this.pocev_od = username;
    }
     public String getTrajanje() {
        return trajanje;
    }

    public void setTrajanje(String trajanje) {
        this.trajanje = trajanje;
    }

   public int compareTo(Object o){
        Time t1 = start;
        Time t2 = ((Rezervacija)o).getStart();
        if(t1.after(t2)){
            return 1;
        }
        else{
            return -1;
        }
    }

   public String otkazi(){
       
        String vrati="";
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        Connection con = null;
        ResultSet rs = null;
        Statement stmt = null;
        try {
            con = DB.getConnection();
            stmt = con.createStatement();
            rs = stmt.executeQuery("select sid from sala where naziv='"+sala+"';");
            int sid=-1;
            if(rs.next())
                sid = rs.getInt("sid");
            stmt.executeUpdate("delete from rezervacija where rid='"+rid+"' ;");
            ArrayList<Rezervacija> rezs = (ArrayList<Rezervacija>)session.getAttribute("korrez");
            for(int i=0;i<rezs.size();i++)
                if( ((Rezervacija)rezs.get(i)).equals(this) ){
                    rezs.remove(i);
                    i--;
                }
            session.setAttribute("korrez", rezs);
            stmt.close();con.close();
        }catch (Exception e) {
            if (con != null) {
                try {
                    con.close();
                } catch (Exception ex) {}
            }
            context.addMessage(null, new FacesMessage(e.getLocalizedMessage()));
            vrati="error";
            return vrati;
        }
        User u=(User)session.getAttribute("user");
        return u.adminuserp();

   }
}
